<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            
            
            
            <link rel="shortcut icon" href="../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <title>Bmob文档中心</title>
            <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../css/highlight.css">
            <link href="../../css/agate.css" rel="stylesheet">
            <link href="../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../..">
                <!--Bmob文档中心-->
                <img src="../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../..">文档首页</a>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../data/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/ios/">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/restful/">RESTful</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/javascript/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../cloud_function/android/">云函数</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../cloud_function/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/restful/">RESTful</a>
                                </li>
							
                            
								<li >
									<a href="../../cloud_function/web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/restful/">RESTful</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">IM服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">IM服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="../android/">推送服务</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../javascript/">JavaScript</a>
                                </li>
							
                            
                                <li class="active">
                                    <a href="./">RESTful</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
				
				
				
				
					<div class="code-title">RESTful</div>
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
		
			
		
			
		
			
		
			
		
			
				
					
						
					
						
					
						
					
						
					
						
							
								
									
										<li class="active "><a href="#_1">消息推送简介</a>
											
										</li>
									
										<li class=""><a href="#_2">安装消息推送服务</a>
											
										</li>
									
										<li class=""><a href="#installation">保存 installation</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#iosdevicetoken">保存iOS设备的deviceToken</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#android-installationid">保存Android设备的 installationId</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#windows-phone-notificationuri">保存Windows Phone设备的 notificationUri</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#_3">订阅频道和退订</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_4">订阅频道</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_5">退订频道</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#_6">推送消息</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_7">广播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_8">组播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_9">多播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_14">点播推送消息</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#_15">发送选项设置</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_16">定制你的通知</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#_17">设置消息过期时间</a></li>
											</ul>
											
										</li>
									
								
							
						
					
				
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<h2 id="_1">消息推送简介<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h2>
<p>推送通知是让用户及时被通知、和你的应用保持联系的一种非常棒的方式，你可以快速而有效地通知到所有的用户，下面这个教程将会教你如何使用Bmob来推送消息。</p>
<h2 id="_2">安装消息推送服务<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h2>
<p>每一个Bmob的App被安装在用户的设备上后，如果要使用消息推送功能，Bmob SDK会自动生成一个Installation对象。Installation对象包含了推送所需要的所有信息。举例：一个棒球的App，你可以让用户订阅感兴趣的棒球队，然后及时将这个球队的消息推送给用户 。
你可以使用 REST API，通过请求URL <strong>/1/installations</strong> 对Installation对象进行一系列操作，就像你存储和获取其他的普通对象，如GameScore对象一样。
注意Installation对象一般只在Client SDK中进行修改，但REST API对于大量导入或导出订阅数据的Installation对象是极其有用的。</p>
<p>Installation对象有几个系统默认的特殊字段来帮助你进行定位到设备等管理：
- <strong>badge</strong> : iOS应用中右上角的图标标识，这不会在服务端进行自增，需要开发者自己实现自增统计
- <strong>channels</strong> : 当前这个设备订阅的渠道名称数组
- <strong>timeZone</strong> : 设备所在位置的时区， 如Asia/Shanghai，这个会在每个Installation对象更新时同步（只读）
- <strong>deviceType</strong> : 设备的类型, 值为："ios" 或 "android" (只读)
- <strong>installationId</strong> : Bmob使用的设备唯一号，Android设备是必须的，iOS可选 (只读)
- <strong>deviceToken</strong> : iOS设备由Apple APNS生成的唯一性token标识 (只读)
- <strong>notificationUri</strong> : Windows Phone设备由Windows Push Center生成的唯一性token标识</p>
<h2 id="installation">保存 installation<a class="headerlink" href="#installation" title="Permanent link">&para;</a></h2>
<p>根据设备的不同往设备表中添加不同的值，body格式如下</p>
<pre><code>{
  &quot;deviceType&quot;: deviceType (&quot;iOS&quot;,&quot;android&quot;, &quot;windows phone&quot;)
  &quot;deviceToken&quot;: iOS需填
  &quot;installationId&quot;:android需填
  &quot;notificationUri&quot;:wp需填
}
</code></pre>

<p>下面将针对具体平台分别举例。</p>
<h3 id="iosdevicetoken">保存iOS设备的deviceToken<a class="headerlink" href="#iosdevicetoken" title="Permanent link">&para;</a></h3>
<p>iOS设备通常使用deviceToken来对一台设备进行惟一标识。</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;deviceType&quot;: &quot;ios&quot;,
        &quot;deviceToken&quot;: &quot;abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789&quot;
      }' \
  https://api.bmob.cn/1/installations
</code></pre>

<h3 id="android-installationid">保存Android设备的 installationId<a class="headerlink" href="#android-installationid" title="Permanent link">&para;</a></h3>
<p>对于Android设备，<a href="http://www.bmob.cn/" title="Bmob移动后端云服务平台">Bmob</a>SDK会自动生成uuid作为installationId保存到 Bmob。 你可以使用以下REST API保存Android设备的installation ID。</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;deviceType&quot;: &quot;android&quot;,
        &quot;installationId&quot;: &quot;12345678-4312-1234-1234-1234567890ab&quot;
      }' \
  https://api.bmob.cn/1/installations
</code></pre>

<h3 id="windows-phone-notificationuri">保存Windows Phone设备的 notificationUri<a class="headerlink" href="#windows-phone-notificationuri" title="Permanent link">&para;</a></h3>
<p>对于Windows Phone设备，<a href="http://www.bmob.cn/" title="Bmob移动后端云服务平台">Bmob</a>SDK会自动从Windows Push Center获取到notificationUri并保存到 Bmob。 你可以使用以下REST API保存Windows Phone设备的notificationUri。</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;deviceType&quot;: &quot;windows phone&quot;,
        &quot;notificationUri&quot;: &quot;https://hk1.notify.live.net/unthrottledthirdparty/01.00/AQHgHh6EuwNtTa1TbwvaAjnmAgAAAAADEAAAAAQUZm52OkZDM0U5RDkxQzIzREJBNDYFBkFTRUEwMQ&quot;
      }' \
  https://api.bmob.cn/1/installations
</code></pre>

<h2 id="_3">订阅频道和退订<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h2>
<p>频道列的本质是一个数组，因此其操作与数组一致，具体可查看以下举例。</p>
<h3 id="_4">订阅频道<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h3>
<p>覆盖已订阅的频道：</p>
<pre><code>curl -X PUT \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;channels&quot;: [
          &quot;Giants&quot;,
          &quot;Mets&quot;
        ]
      }' \
  https://api.bmob.cn/1/installations/mrmBZvsErB
</code></pre>

<p>订阅一个新频道，不覆盖已订阅的频道：</p>
<pre><code>curl -X PUT \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{&quot;channels&quot;:{&quot;__op&quot;:&quot;AddUnique&quot;,&quot;objects&quot;:[&quot;Giants&quot;]}}' \
  https://api.bmob.cn/1/installations/mrmBZvsErB
</code></pre>

<h3 id="_5">退订频道<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h3>
<pre><code>curl -X PUT \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{&quot;channels&quot;:{&quot;__op&quot;:&quot;Remove&quot;,&quot;objects&quot;:[&quot;Giants&quot;，&quot;Mets&quot;]}}' \
  https://api.bmob.cn/1/installations/mrmBZvsErB
</code></pre>

<h2 id="_6">推送消息<a class="headerlink" href="#_6" title="Permanent link">&para;</a></h2>
<p><strong>请求描述</strong></p>
<p>使用推送接口可将消息推送至对应设备。</p>
<p><strong>请求</strong></p>
<ul>
<li>
<p>url ： https://api.bmob.cn/1/push</p>
</li>
<li>
<p>method ：POST</p>
</li>
<li>
<p>header:</p>
</li>
</ul>
<pre><code>X-Bmob-Application-Id: Your Application ID
X-Bmob-REST-API-Key: Your REST API Key
Content-Type: application/json
</code></pre>

<ul>
<li>body: 根据不同的需求进行定制，具体看以下介绍。</li>
</ul>
<h3 id="_7">广播推送消息<a class="headerlink" href="#_7" title="Permanent link">&para;</a></h3>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Hello From Bmob.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h3 id="_8">组播推送消息<a class="headerlink" href="#_8" title="Permanent link">&para;</a></h3>
<p>发送给订阅了Giants频道的用户</p>
<pre><code> curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;:{
          &quot;channels&quot;:[&quot;Giants&quot;]
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Hello From Bmob.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>或者更简便的方式</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;channels&quot;:[&quot;Giants&quot;],
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Hello From Bmob.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h3 id="_9">多播推送消息<a class="headerlink" href="#_9" title="Permanent link">&para;</a></h3>
<h4 id="_10">推送给不活跃的用户<a class="headerlink" href="#_10" title="Permanent link">&para;</a></h4>
<pre><code>  curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;:{
            &quot;updatedAt&quot;:{
                &quot;$lt&quot;:{&quot;__type&quot;:&quot;Date&quot;,&quot;iso&quot;:&quot;2012-01-29 11:33:53&quot;}
              }
        },
        &quot;data&quot;: {
            &quot;alert&quot;: &quot;Hello From Bmob.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h4 id="_11">根据查询条件做推送<a class="headerlink" href="#_11" title="Permanent link">&para;</a></h4>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;scores&quot;: true
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Willie Hayes injured by own pop fly.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>请注意，where 条件查询的都是 installations 表。这里是假设 installations 表存储了 scores 的布尔属性，你可以像查询普通对象一样构造where查询</p>
<h4 id="_12">根据平台做推送<a class="headerlink" href="#_12" title="Permanent link">&para;</a></h4>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;deviceType&quot;: &quot;android&quot;
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Your suitcase has been filled with tiny robots!&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;deviceType&quot;: &quot;ios&quot;
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Your suitcase has been filled with tiny apples!&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>Windows Phone平台的推送分三种格式：</p>
<p>a.推送Raw通知(<strong>默认</strong>)：</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;deviceType&quot;: &quot;windows phone&quot;
        },
        &quot;data&quot;: {
           &quot;alert&quot;: &quot;Your suitcase has been filled with tiny windows phone!&quot;,
           &quot;wpAlert&quot;: &quot;TextBoxValue2&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>b.推送Toast通知：</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;deviceType&quot;: &quot;windows phone&quot;
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Your suitcase has been filled with tiny apples!&quot;,
          &quot;wpAlert&quot;: &quot;TextBoxSubTitle&quot;,
          &quot;wp&quot;: 2
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>c.推送磁贴通知：</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;deviceType&quot;: &quot;windows phone&quot;
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Your suitcase has been filled with tiny apples!&quot;,
          &quot;backgroundImage&quot;: &quot;backgroundImage&quot;,
          &quot;count&quot;:&quot;1&quot;,
          &quot;backBackgroundImage&quot;: &quot;backBackgroundImage&quot;,
          &quot;backTitle&quot;:&quot;backTitle&quot;,
          &quot;backContent&quot;:&quot;backContent&quot;,
          &quot;wp&quot;: 1
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h4 id="_13">根据地理信息位置做推送<a class="headerlink" href="#_13" title="Permanent link">&para;</a></h4>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;: {
          &quot;user&quot;: {
            &quot;$inQuery&quot;: {
              &quot;location&quot;: {
                &quot;$nearSphere&quot;: {
                  &quot;__type&quot;: &quot;GeoPoint&quot;,
                  &quot;latitude&quot;: 30.0,
                  &quot;longitude&quot;: -20.0
                },
                &quot;$maxDistanceInMiles&quot;: 1.0
              }
            }
          }
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Free hotdogs at the bmob concession stand!&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>上面的例子假设 installation 有个 user 属性指向_User 表的记录，并且用户有个 location 属性是 GeoPoint 类型，我们就可以根据地理信息位置做推送。</p>
<h3 id="_14">点播推送消息<a class="headerlink" href="#_14" title="Permanent link">&para;</a></h3>
<p>发送给Android单个客户端</p>
<pre><code>  curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;:{
            &quot;installationId&quot;:&quot;57234d4c-752f-4e78-81ad-a6d14048020d&quot;
        },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Hello From Bmob.&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>发送给iOS单个客户端</p>
<pre><code>  curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;:{
            &quot;deviceToken&quot;:&quot;abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789&quot;
         },
        &quot;data&quot;: {
          &quot;aps&quot;:{&quot;alert&quot;:&quot;cashi&quot;,&quot;badge&quot;:1,&quot;sound&quot;:&quot;default&quot;}
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>发送给Windows Phone单个客户端Toast通知</p>
<pre><code>  curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;where&quot;:{
            &quot;notificationUri&quot;:&quot;https://hk1.notify.live.net/unthrottledthirdparty/01.00/AQHgHh6EuwNtTa1TbwvaAjnmAgAAAAADEAAAAAQUZm52OkZDM0U5RDkxQzIzREJBNDYFBkFTRUEwMQ&quot;
         },
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Hello From bmob&quot;,
          &quot;wpAlert&quot;: &quot;bmob&quot;,
          &quot;wp&quot;: 2
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h2 id="_15">发送选项设置<a class="headerlink" href="#_15" title="Permanent link">&para;</a></h2>
<p>推送通知不仅仅可以用来发送消息，iOS中，推送内容还可以包含要播放的声音和要显示的badge数字值,还可以定制任何你想发送的内容，对于Android，你甚至可以指定Intent应用于接收消息，对于时效有要求的消息你还可以设置一个消息过期时间。</p>
<h3 id="_16">定制你的通知<a class="headerlink" href="#_16" title="Permanent link">&para;</a></h3>
<p>如果你想发送更多的消息，你可以在data的字典对象中设置其他字段，下面这些保留的字段有其特殊的意义：
- <strong>alert</strong> : 通知的消息内容
- <strong>badge</strong> : (仅iOS)一个数字值将会高亮显示在应用图标的右上角。
- <strong>sound</strong> : (仅iOS)应用绑定包中的声音
- <strong>content-available</strong> : (仅iOS)如果你的应用是新闻类的，或者你的应用正在使用iOS7的Remote Notification Background Mode，设定这个值为1将会触发离线下载功能。
- <strong>action</strong> : (仅Android)接收到推送消息时应用Intent，如果没有指定title或alert，Intent将被应用，但是没有通知呈现给用户。
- <strong>title</strong> : (仅Android)这个值将显示在系统通知栏
- <strong>wp</strong> : (仅Windows Phone)通知类型 0-Raw(默认) 1-Toast 2-磁贴
- <strong>wpAlert</strong> : (仅Windows Phone)这个值将在Raw 和 Toast两种通知中有用，相对alert，作为子标题
- <strong>backgroundImage</strong> : (仅Windows Phone)通知类型wp=2，磁贴类通知中有用
- <strong>count</strong> : (仅Windows Phone)通知类型wp=2，磁贴类通知中有用
- <strong>backBackgroundImage</strong> : (仅Windows Phone)通知类型wp=2，磁贴类通知中有用
- <strong>backTitle</strong> : (仅Windows Phone)通知类型wp=2，磁贴类通知中有用
- <strong>backContent</strong> : (仅Windows Phone)通知类型wp=2，磁贴类通知中有用</p>
<p>例如，推送一条通知，将当前的badge值设置为一个数字值,并且播放iOS设备上定制的音乐，对于Android用户，将在系统通知栏中显示特殊的标题，其请求如下：</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;channels&quot;: [
          &quot;Mets&quot;
        ],
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;The Mets scored! The game is now tied 1-1.&quot;,
          &quot;badge&quot;: 10,
          &quot;sound&quot;: &quot;cheering.caf&quot;,
          &quot;title&quot;: &quot;Mets Score!&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>当然，你也可以在data字典对象中定制自己的字段来推送消息，但推送到消息不显示在 Andoid 系统的通知栏中，而是执行应用程序预定义的逻辑，Android或iOS的接收消息文档中对此有说明，iOS只有当用户从通知中打开应用，才能访问data的数据，Android将提供这个数据在action Intent应用之后将提供这个数据,</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot; \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot; \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;channels&quot;: [
          &quot;Indians&quot;
        ],
        &quot;data&quot;: {
          &quot;action&quot;: &quot;com.example.UPDATE_STATUS&quot;,
          &quot;alert&quot;: &quot;Ricky Vaughn was injured in last night's game!&quot;,
          &quot;name&quot;: &quot;Vaughn&quot;,
          &quot;newsItem&quot;: &quot;Man bites dog&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<h3 id="_17">设置消息过期时间<a class="headerlink" href="#_17" title="Permanent link">&para;</a></h3>
<p>过期时间，可以是绝对时间：</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;expiration_time&quot;: &quot;2013-12-04 00:51:13&quot;,
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Season tickets on sale until December  4, 2013&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>

<p>也可以是相对时间（根据push_time做定期推送，从push_time时间开始算起，直到expiration_interval时间后过期），下面的请求表示消息将在2012-01-28 00:51:13这个时间点开始推送，直到7天后过期:</p>
<pre><code>curl -X POST \
  -H &quot;X-Bmob-Application-Id: Your Application ID&quot;          \
  -H &quot;X-Bmob-REST-API-Key: Your REST API Key&quot;        \
  -H &quot;Content-Type: application/json&quot; \
  -d '{
        &quot;push_time&quot;: &quot;2012-01-28 00:51:13&quot;,
        &quot;expiration_interval&quot;: 518400,
        &quot;data&quot;: {
          &quot;alert&quot;: &quot;Season tickets on sale until December  4, 2013&quot;
        }
      }' \
  https://api.bmob.cn/1/push
</code></pre>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../..';</script>
            <script src="../../js/jquery-1.10.2.min.js"></script>
            <script src="../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../js/highlight.pack.js"></script>
            <script src="../../js/main.js"></script>
            <script src="../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>